﻿using System.Windows;
using AdRotator;
using Microsoft.Phone.Controls;


namespace AdRotatorExample
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            AdRotatorControl.SlidingAdDisplaySeconds = 4;
            AdRotatorControl.SlidingAdHiddenSeconds = 2;
            SlidingAdDirectionSelector.Items.Add(SlideDirection.None);
            SlidingAdDirectionSelector.Items.Add(SlideDirection.Left);
            SlidingAdDirectionSelector.Items.Add(SlideDirection.Top);
            SlidingAdDirectionSelector.Items.Add(SlideDirection.Right);
            SlidingAdDirectionSelector.Items.Add(SlideDirection.Bottom);
            AdRotatorControl.Log += new AdRotator.AdRotatorControl.LogHandler(AdRotatorControl_Log);


        }

        void AdRotatorControl_Log(string message)
        {
            Dispatcher.BeginInvoke(() => MessagesListBox.Items.Insert(0,message));
        }

        private void Random_Button_Click(object sender, RoutedEventArgs e)
        {
          PageTitle.Text =  AdRotatorControl.Invalidate();
        }

        private void Next_Button_Click(object sender, RoutedEventArgs e)
        {
            PageTitle.Text = AdRotatorControl.Invalidate(true);
        }
		
		protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            AdRotatorControl.DefaultHouseAdBody = new MyDefaultAd();
            if (AdRotatorControl.IsInitilised)
            {
                AdRotatorControl.Invalidate();
            } 
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            AdRotatorControl.Dispose();
            base.OnNavigatedFrom(e);
        }
    }
}